import 'dart:async';

import 'package:flutter_menu/biz/setting/setting_controller.dart';
import 'package:flutter_menu/gen/assets.gen.dart';
import 'package:flutter_menu/widgets/comm_dialog.dart';
import 'package:get/get.dart';
import 'package:r_upgrade/r_upgrade.dart';
import 'package:retrofit/http.dart';

import '../../res/app_design.dart';

void onNewApp() {}




class UpdateWidget extends CommDialog {
  final SettingController _controller = Get.find();
  UpdateWidget({super.key});

  @override
  Widget body(BuildContext context) {
    return GetBuilder<SettingController>(
        init: _controller,
        id: "updateApp",
        builder: (controller) => DefaultTextStyle(
              style: AppStyle.app_text.copyWith(fontSize: AppDimen.sp_25, color: mainColor),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [genTitleWidget("check_version".tr), _process(), _bottomBtn()],
              ),
            ));
  }

  Widget _process() {
    var type = _controller.status;
    var cr = const Color(0xff68C6EA);
    var dsc = "downloading".tr;
    var ind = MyAssets.images.icUpdateDownloading;
    int pos = _controller.process > 100 ? 100 : _controller.process;
    if (DownStatus.error == type) {
      cr = const Color(0xffEC4D4A);
      dsc = "update_error".tr;
      ind = MyAssets.images.icUpdateFail;
      pos = 0 ;
    } else if (DownStatus.complete == type) {
      cr = const Color(0xff54B870);
      dsc = "downloaded".tr;
      ind = MyAssets.images.icUpdateSuccess;
      pos = 100 ;
    }

    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        LayoutBuilder(
          builder: (BuildContext context, BoxConstraints constraints) {
            double div = (constraints.maxWidth - 47.w) / 100;

            return Column(
              children: [
                Stack(
                  children: [
                    Positioned(
                        top: 0,
                        left: div * pos,
                        child: Text(
                          "$pos",
                          style: TextStyle(color: cr, fontSize: 25.sp),
                        ))
                  ],
                ).size(width: double.infinity, height: 40.w),
                Stack(
                  children: [
                    Positioned(
                        child: Container(
                            margin: EdgeInsets.symmetric(vertical: 9.w),
                            height: 29.w,
                            decoration: BoxDecoration(
                              color: cr, // solid color为#ffb8b8b8
                              borderRadius: BorderRadius.circular(30.w), // corners radius为30dp
                            ))),
                    Positioned(left: div * pos, child: ind.image(width: 47.w, height: 47.w)),
                  ],
                ),
              ],
            );
          },
        ),
        Text(
          dsc,
          style: TextStyle(color: cr, fontSize: 25.sp),
        ).marginOnly(top: 15.w)
      ],
    ).marginOnly(bottom: 91.w, top: 47.w);
  }

  Widget _bottomBtn() {
    List<Widget> children = [];

    switch (_controller.status) {
      case DownStatus.error:
        children = [
          genBtn("cancel".tr, confirm: false, onTap:()=>Get.back(), width: 209.w)
              .marginOnly(right: 15.w),
          genBtn("retry".tr, onTap: _controller.retry)
        ];
        break;
      case DownStatus.loading:
        children = [genBtn("download_background".tr, onTap: ()=>Get.back())];
        break;

      case DownStatus.complete:
        children = [genBtn("install".tr, onTap: _controller.install)];
        break;
    }

    return Row(
      children: children,
    ).paddingOnly(top: 25.w, bottom: 25.w);
  }
}


enum DownStatus {
  loading,
  error,
  complete;
}
